//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Services.Localization;
namespace DotNetNuke.UI.Containers
{
	partial class Title : UI.Skins.SkinObjectBase
	{
		private string _cssClass;
		const string MyFileName = "Title.ascx";
		public string CssClass {
			get { return _cssClass; }
			set { _cssClass = value; }
		}
		private bool CanEditModule()
		{
			bool blnCanEdit = false;
			if ((ModuleControl != null) && (ModuleControl.ModuleContext.ModuleId > Null.NullInteger)) {
				blnCanEdit = (PortalSettings.UserMode == DotNetNuke.Entities.Portals.PortalSettings.Mode.Edit) && TabPermissionController.CanAdminPage() && !Common.Globals.IsAdminControl();
			}
			return blnCanEdit;
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			lblTitle.UpdateLabel += new DotNetNuke.UI.WebControls.DNNLabelEdit.DNNLabelEditEventHandler(lblTitle_UpdateLabel);
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			try {
				if (!String.IsNullOrEmpty(CssClass)) {
					lblTitle.CssClass = CssClass;
				}
				string strTitle = Null.NullString;
				if (ModuleControl != null) {
					strTitle = Localization.LocalizeControlTitle(ModuleControl);
				}
				if (strTitle == Null.NullString) {
					strTitle = "&nbsp;";
				}
				lblTitle.Text = strTitle;
				lblTitle.EditEnabled = false;
				tbEIPTitle.Visible = false;
				if (CanEditModule() == true && PortalSettings.InlineEditorEnabled == true) {
					lblTitle.EditEnabled = true;
					tbEIPTitle.Visible = true;
				} else {
					foreach (WebControls.DNNToolBarButton objButton in this.tbEIPTitle.Buttons) {
						objButton.ToolTip = Localization.GetString("cmd" + objButton.ToolTip, Services.Localization.Localization.GetResourceFile(this, MyFileName));
					}
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void lblTitle_UpdateLabel(object source, WebControls.DNNLabelEditEventArgs e)
		{
			if (CanEditModule()) {
				DotNetNuke.Entities.Modules.ModuleController objModule = new DotNetNuke.Entities.Modules.ModuleController();
				DotNetNuke.Entities.Modules.ModuleInfo objModInfo = objModule.GetModule(ModuleControl.ModuleContext.ModuleId, ModuleControl.ModuleContext.TabId, false);
				objModInfo.ModuleTitle = e.Text;
				objModule.UpdateModule(objModInfo);
			}
		}
	}
}
